package com.xindz.hgq.database.greendao.view;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.blankj.utilcode.constant.TimeConstants;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.orhanobut.logger.Logger;
import com.xindz.hgq.R;
import com.xindz.hgq.adapter.GdUserAdapter;
import com.xindz.hgq.app.App;
import com.xindz.hgq.base.BaseTitleActivity;
import com.xindz.hgq.base.options.BaseActivityOptions;
import com.xindz.hgq.database.greendao.bean.DaoSession;
import com.xindz.hgq.database.greendao.bean.User;
import com.xindz.hgq.database.greendao.bean.UserDao;

import org.greenrobot.greendao.async.AsyncOperation;
import org.greenrobot.greendao.async.AsyncOperationListener;
import org.greenrobot.greendao.async.AsyncSession;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @name HGQBase
 * @class name：com.xindz.hgq.database.greendao.view
 * @describe describe
 * @anthor XINDZ
 * @time 2018/5/14 17:32
 * @change
 * @chang time
 * @class describe
 */
public class GdSimplyExampleActivity extends BaseTitleActivity {
    @BindView(R.id.bt_gd_se_insert)
    Button btGdSeInsert;
    @BindView(R.id.bt_gd_se_delete)
    Button btGdSeDelete;
    @BindView(R.id.bt_gd_se_update)
    Button btGdSeUpdate;
    @BindView(R.id.bt_gd_se_query)
    Button btGdSeQuery;
    @BindView(R.id.rcv_gd_se)
    RecyclerView rcvGdSe;
    @BindView(R.id.bt_gd_se_delete_all)
    Button btGdSeDeleteAll;
    @BindView(R.id.bt_gd_se_query_all)
    Button btGdSeQueryAll;
    private ArrayList<User> list = new ArrayList<>();
    ;
    private GdUserAdapter adapter;
    private String objId;

    private int name = 0;
    private DaoSession daoSession;
    private UserDao userDao;

    @Override
    public BaseActivityOptions setOption() {
        return new BaseActivityOptions().openTitle();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gd_simply_example);
        ButterKnife.bind(this);
        setTitleText("GreenDAO 简单例子");
        showLeftBack();
        rcvGdSe.setLayoutManager(new LinearLayoutManager(this));
        adapter = new GdUserAdapter(R.layout.item_gd_user, list);
        rcvGdSe.setAdapter(adapter);
        daoSession = App.app.getDaoSession();
        userDao = daoSession.getUserDao();
    }


    @OnClick({R.id.bt_gd_se_insert, R.id.bt_gd_se_delete, R.id.bt_gd_se_update,
            R.id.bt_gd_se_query, R.id.bt_gd_se_delete_all, R.id.bt_gd_se_query_all})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_gd_se_insert:
                User newUser = createNewUser();
                userDao.insert(newUser);
                ToastUtils.showShort("添加成功");
                break;
            case R.id.bt_gd_se_delete:
                User userDelete = null;
                if (list.size() > 0) {
                    userDelete = list.get(list.size() - 1);
                }
                if (userDelete != null) {
                    userDao.delete(userDelete);
                    ToastUtils.showShort("删除成功");
                } else {
                    ToastUtils.showShort("删除失败");

                }

                break;
            case R.id.bt_gd_se_delete_all:
                userDao.deleteAll();
                ToastUtils.showShort("删除全部");
                break;
            case R.id.bt_gd_se_update:
                User userUpdate = null;
                if (list.size() > 0) {
                    userUpdate = list.get(list.size() - 1);
                }

                if (userUpdate != null) {
                    String yname = userUpdate.getUsername();
                    userUpdate.setUsername(yname + " 已修改");
                    userDao.update(userUpdate);
                    ToastUtils.showShort("更新成功");
                } else {
                    ToastUtils.showShort("更新失败");
                }

                break;
            case R.id.bt_gd_se_query:
                List<User> queryList = userDao.queryBuilder().where(UserDao.Properties.ObjecId.eq(objId)).list();
                if (queryList != null) {
                    list.clear();
                    list.addAll(queryList);
                    adapter.notifyDataSetChanged();
                    ToastUtils.showShort("查询成功");
                } else {
                    ToastUtils.showShort("查询失败");

                }
                break;

            case R.id.bt_gd_se_query_all:

                List<User> users = userDao.loadAll();
                if (users != null) {
                    ToastUtils.showShort("查询到 " + users.size() + " 条数据");
                    this.list.clear();
                    this.list.addAll(users);
                    adapter.notifyDataSetChanged();
                } else {
                    ToastUtils.showShort("没有查询到数据");
                }

//                AsyncSession asyncSession = daoSession.startAsyncSession();
//                asyncSession.loadAll(User.class);
//                asyncSession.setListener(new AsyncOperationListener() {
//                    @Override
//                    public void onAsyncOperationCompleted(AsyncOperation operation) {
//
//                    }
//                });
                break;
        }
    }

    private User createNewUser() {
        User user = new User();
        long timeMillis = System.currentTimeMillis();
        objId = timeMillis + "";
        user.setObjecId(objId);
        user.setUsername("第" + name + "次添加");
        user.setSex(1);
        name++;
        Date dateByNow = TimeUtils.getDateByNow(timeMillis, TimeConstants.MSEC);
        Logger.i("时间:" + dateByNow.toString());
        user.setBirthday(dateByNow);
        user.setMobilePhoneNumber("10086" + name);
        user.setAvatar("图片路径:"+name);
        user.setMobilePhoneVerified(false);
        return user;
    }


}
